import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtUiTools import QUiLoader
from mainUi.WinControl import WinControl, EventType
from qt_material import apply_stylesheet


# noinspection PyUnresolvedReferences
class ToolsMainWindow:
    # 初始化主窗口
    def __init__(self, myApp):
        # 从文件中加载UI定义
        self.main = QUiLoader().load('main.ui')
        # new 一个窗口控制对象
        self.controller = WinControl(self.main)
        # 绑定窗口数据对象
        self.data = self.controller.data
        # 绑定状态栏
        self.controller.data.SRGLabel = self.main.SRGLabel
        # 绑定 QApplication 对象
        self.app = myApp
        # 初始化窗体所有默认数据
        self.InitWindowData()
        # 初始化所有窗体事件
        self.InitEvent()
        # 设置窗口主题
        self.SetPanel()

    # 定义窗口数据初始化
    def InitWindowData(self):
        # # 设置 需要搜索的Excl表格
        # self.main.SComboBox.addItems(self.data.exclList)
        # 设置 Excl表格默认路径
        self.main.SExclPathEdit.setText(self.data.SExclPath)

        # # 设置 批量检查列表默认内容
        # self.main.CExcelListWidget.addItems(self.data.exclList)
        # # 设置 策略检查列表默认内容
        # self.main.TSelectComboBox.addItems(self.data.TList)
        # # 设置 策略检查列表默认内容
        # self.main.TListWidget.addItems(self.data.TExclList)

    # 定义绑定窗口事件初始化
    def InitEvent(self):
        # 绑定菜单栏主题设置 点击事件
        self.main.panel.triggered.connect(self.PanelSettingEvent)
        # 绑定菜单栏默认设置 点击事件
        self.main.setting.triggered.connect(self.SettingsEvent)
        # 绑定菜单栏颜色设置 点击事件
        self.main.highColor.triggered.connect(self.ColorSettingEvent)
        # 绑定菜单栏字体大小设置 点击事件
        self.main.highSize.triggered.connect(self.SizeSettingEvent)
        # 绑定菜单栏EXCEL读取设置 点击事件
        self.main.excelSetting.triggered.connect(self.ExcelSettingEvent)

        # 1.查询界面
        # # 绑定Excel下拉框 选择事件
        # self.main.SComboBox.currentIndexChanged.connect(self.SExclComboBoxEvent)
        # 绑定Excel路径输入框 回车事件
        self.main.SExclPathEdit.returnPressed.connect(self.SExclPathEdit_RP_Event)
        # 绑定Excel路径输入框 文本变更事件
        self.main.SExclPathEdit.editingFinished.connect(self.SExclPathEdit_TC_Event)
        # 绑定Excel路径选择按钮 点击事件
        self.main.SSelectButton.clicked.connect(self.SSelectButtonEvent)
        # 绑定单个数据按钮 点击事件
        # self.main.SBaseButton.clicked.connect(self.SBaseButtonEvent)
        # 绑定活动数据按钮 点击事件
        self.main.SActivityButton.clicked.connect(self.SActivityButtonEvent)
        # # 绑定添加查询列数据按钮 点击事件
        # self.main.SAddButton.clicked.connect(self.SAddButtonEvent)
        # # 绑定单个表格重载按钮 点击事件
        # self.main.SUpDataButton.clicked.connect(self.SUpDataButtonEvent)
        # 绑定数据比对详情弹窗按钮 点击事件
        self.main.SLookCheckButton.clicked.connect(self.SLookCheckButtonEvent)
        # 绑定规划输入框 文本变更事件
        self.main.SResourceEdit.textChanged.connect(self.SResourceEditEvent)
        # 绑定基础表输入框 文本变更事件
        self.main.STargetEdit.textChanged.connect(self.STargetEditEvent)

        # # 2.检查界面
        # # 绑定检查Excel按钮 点击事件
        # self.main.CStartButton.clicked.connect(self.CStartButtonEvent)
        #
        # # 3.策略界面
        # # 绑定策略下拉框 选择事件
        # self.main.TSelectComboBox.currentIndexChanged.connect(self.TSelectComboBoxEvent)
        # # 绑定策略新增按钮 点击事件
        # self.main.TAddButton.clicked.connect(self.TAddButtonEvent)
        # # 绑定策略检查按钮 点击事件
        # self.main.TStartButton.clicked.connect(self.TStartButtonEvent)

    # 1.查询界面
    # Excel选择 下拉框 选择事件监听
    def SExclComboBoxEvent(self):
        self.controller.SendEvent(self.main.SComboBox, EventType.SExclComboBox)

    # Excel路径 输入框 回车事件监听
    def SExclPathEdit_RP_Event(self):
        self.controller.SendEvent(self.main.SExclPathEdit, EventType.SExclPathEdit_rp)

    # Excel路径 输入框 文本变更事件监听
    def SExclPathEdit_TC_Event(self):
        self.controller.SendEvent(self.main.SExclPathEdit, EventType.SExclPathEdit_tc)

    # Excel路径选择 按钮 点击事件监听
    def SSelectButtonEvent(self):
        self.controller.SendEvent(self.main.SExclPathEdit, EventType.SSelectButton)

    # 单个数据 按钮 点击事件监听
    def SBaseButtonEvent(self):
        ...
        # self.data.SBaseEdit = self.main.SBaseEdit.text()
        # self.controller.SendEvent(self.main.SInfoTableWidget, EventType.SBaseButton)

    # 活动数据 按钮 点击事件监听
    def SActivityButtonEvent(self):
        self.data.SResourceEdit = self.main.SResourceEdit.toPlainText()
        self.data.STargetEdit = self.main.STargetEdit.toPlainText()
        if self.data.SResourceEdit:
            self.data.SDataRadioButton = self.main.SDataRadioButton.isChecked()  # P4,P6规划检查 RadioButton
            self.data.P7DataRadioButton = self.main.P7DataRadioButton.isChecked()  # P7规划检查 RadioButton
            self.data.SStrRadioButton = self.main.SStrRadioButton.isChecked()  # 纯文本比对检查 RadioButton
            self.data.SGoDataCheckBox = self.main.SGoDataCheckBox.isChecked()  # 自动弹窗 CheckBox
            self.data.SCostsComboBox = self.main.SCostsComboBox.currentText() if \
                self.main.SCostsComboBox.currentText() != "无兑换消耗" else None  # 兑换消耗 ComboBox
            self.controller.SendEvent(self.main.SDiffTextEdit, EventType.SActivityButton,
                                      [self.main.SResourceEdit, self.main.STargetEdit])
        else:
            self.controller.SendEvent(self.main.SDiffTextEdit, EventType.SBaseButton)

    # 添加查询列数据 按钮 点击事件监听
    def SAddButtonEvent(self):
        self.controller.SendEvent(self.main.SAddButton, EventType.SAddButton)

    # 单个表格重载 按钮 点击事件监听
    def SUpDataButtonEvent(self):
        self.controller.SendEvent(self.main.SUpDataButton, EventType.SUpDataButton)

    # 数据比对详情弹窗 按钮 点击事件
    def SLookCheckButtonEvent(self):
        self.controller.SendEvent(self.main.SLookCheckButton, EventType.SLookCheckButton)

    # 规划输入框 文本变更事件
    def SResourceEditEvent(self):
        self.controller.SendEvent(self.main.SResourceEdit, EventType.SResourceEdit)

    # 基础表输入框 文本变更事件
    def STargetEditEvent(self):
        self.data.SStrRadioButton = self.main.SStrRadioButton.isChecked()  # 纯文本比对检查 RadioButton
        self.data.SCostsComboBox = self.main.SCostsComboBox.currentText() if \
            self.main.SCostsComboBox.currentText() != "无兑换消耗" else None  # 兑换消耗 ComboBox
        self.controller.SendEvent(self.main.STargetEdit, EventType.STargetEdit)

    # # 2.检查界面
    # # 表格检查 按钮 点击事件监听
    # def CStartButtonEvent(self):
    #     self.controller.SendEvent(self.main.CStartButton, EventType.CStartButton)
    #
    # # 3.策略界面
    # # 策略选择 下拉框 选择事件监听
    # def TSelectComboBoxEvent(self):
    #     self.controller.SendEvent(self.main.TSelectComboBox, EventType.TSelectComboBox)
    #
    # # 新增策略 按钮 点击事件监听
    # def TAddButtonEvent(self):
    #     self.controller.SendEvent(self.main.TAddButton, EventType.TAddButton)
    #
    # # 策略检查 按钮 点击事件监听
    # def TStartButtonEvent(self):
    #     self.controller.SendEvent(self.main.TStartButton, EventType.TStartButton)

    # 0.菜单栏设置
    # 高亮颜色 菜单 点击事件监听
    def ColorSettingEvent(self, e):
        highColor = e.text()
        if highColor == 'myColor':
            highColor = self.data.config.highColor

        self.data.highColor = highColor

    # 高亮大小 菜单 点击事件监听
    def SizeSettingEvent(self, e):
        highSize = e.text()
        self.data.highSize = highSize

    # EXCEL读取格式设置菜单 点击事件监听
    def ExcelSettingEvent(self, e):
        self.controller.SendEvent(e, EventType.excelSetting)

    # 主题设置 按钮 点击事件监听
    def PanelSettingEvent(self, e):
        self.data.blackPanel = e.text()
        self.SetPanel()

    # 设置主题
    def SetPanel(self):
        if self.data.blackPanel == "dark_teal":
            self.main.setStyleSheet("*{}")
            apply_stylesheet(self.app, theme='dark_teal.xml')
            # self.main.setStyleSheet('QPlainTextEdit{font: 9pt Microsoft YaHei UI}')
        elif self.data.blackPanel == "light_blue":
            self.main.setStyleSheet("*{}")
            apply_stylesheet(self.app, theme='light_blue.xml')
            # self.main.setStyleSheet('QPlainTextEdit{font: 9pt Microsoft YaHei UI}')
        elif self.data.blackPanel == "light_blue_white":
            apply_stylesheet(self.app, theme='light_blue.xml')
            style = "QTabWidget,QWidget,QPlainTextEdit{ background-color: rgb(240, 240, 240) }"
            # "QPlainTextEdit{font: 9pt Microsoft YaHei UI}"
            self.main.setStyleSheet(style)
        # self.main.setStyleSheet('QPlainTextEdit{font: 9pt Microsoft YaHei UI}')

    # 默认设置 按钮 点击事件监听
    def SettingsEvent(self, e):
        self.controller.SendEvent(e, EventType.settings)


if __name__ == '__main__':
    # new 一个 QApplication 对象，用来承载所有的窗口
    app = QApplication(sys.argv)
    # new 主窗口对象
    main = ToolsMainWindow(app)
    # 展示主窗口
    main.main.show()
    # 通过 QApplication 对象展示主窗口，并等待用户下一步指示
    sys.exit(app.exec())
